package cn.anline.ann_wp.api

import android.app.WallpaperManager
import android.content.*
import android.media.MediaPlayer
import android.service.wallpaper.WallpaperService
import android.text.TextUtils
import android.view.SurfaceHolder
import cn.anline.ann_wp.api.Constant.ACTION_VOICE_NORMAL
import cn.anline.ann_wp.api.Constant.ACTION_VOICE_SILENCE
import java.io.IOException

/**
 * Created by luozh on 2017/7/12.
 */
class VideoWallpaper : WallpaperService() {

    /**
     * 设置壁纸
     * @param context
     */
    fun setToWallPaper(context: Context, videoPath: String) {
        try {
            context.clearWallpaper()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        sVideoPath = videoPath
        val intent = Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER)
        intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, ComponentName(context, VideoWallpaper::class.java))
        context.startActivity(intent)
    }


    override fun onCreateEngine(): WallpaperService.Engine {
        return VideoWallpagerEngine()
    }

    internal inner class VideoWallpagerEngine : WallpaperService.Engine() {
        private var mMediaPlayer: MediaPlayer? = null
        private var mVideoVoiceControlReceiver: BroadcastReceiver? = null

        override fun onCreate(surfaceHolder: SurfaceHolder) {
            super.onCreate(surfaceHolder)
            val intentFilter = IntentFilter(Constant.VIDEO_PARAMS_CONTROL_ACTION)
            mVideoVoiceControlReceiver = VideoVoiceControlReceiver()
            registerReceiver(mVideoVoiceControlReceiver, intentFilter)
        }

        override fun onDestroy() {
            unregisterReceiver(mVideoVoiceControlReceiver)
            super.onDestroy()
        }

        override fun onVisibilityChanged(visible: Boolean) {
            if (visible) {
                mMediaPlayer!!.start()
            } else {
                mMediaPlayer!!.pause()
            }
        }

        override fun onSurfaceCreated(holder: SurfaceHolder) {
            super.onSurfaceCreated(holder)
            if (TextUtils.isEmpty(sVideoPath)) {
                throw NullPointerException("videoPath must not be null ")
            } else {
                mMediaPlayer = MediaPlayer()
                mMediaPlayer!!.setSurface(holder.surface)

                try {

                    mMediaPlayer!!.setDataSource(sVideoPath)
                    mMediaPlayer!!.isLooping = true
                    mMediaPlayer!!.setVolume(0f, 0f)
                    mMediaPlayer!!.prepare()
                    mMediaPlayer!!.start()
                } catch (e: IOException) {
                    e.printStackTrace()
                }

            }

        }

        override fun onSurfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
            super.onSurfaceChanged(holder, format, width, height)
        }

        override fun onSurfaceDestroyed(holder: SurfaceHolder) {
            super.onSurfaceDestroyed(holder)
            if (mMediaPlayer != null) {
                mMediaPlayer!!.release()
                mMediaPlayer = null
            }
        }

        internal inner class VideoVoiceControlReceiver : BroadcastReceiver() {

            override fun onReceive(context: Context, intent: Intent) {
                val action = intent.getIntExtra(Constant.ACTION, -1)
                when (action) {
                    ACTION_VOICE_NORMAL -> mMediaPlayer!!.setVolume(1.0f, 1.0f)
                    ACTION_VOICE_SILENCE -> mMediaPlayer!!.setVolume(0f, 0f)
                }
            }
        }
    }

    companion object {
//        private val TAG = VideoWallpaper::class.java!!.getName()
        private val TAG = "VideoWallpaper Class"
        private var sVideoPath: String? = null
        /**
         * 设置静音
         * @param context
         */
        fun setVoiceSilence(context: Context) {
            val intent = Intent(Constant.VIDEO_PARAMS_CONTROL_ACTION)
            intent.putExtra(Constant.ACTION, ACTION_VOICE_SILENCE)
            context.sendBroadcast(intent)
        }

        /**
         * 设置有声音
         * @param context
         */
        fun setVoiceNormal(context: Context) {
            val intent = Intent(Constant.VIDEO_PARAMS_CONTROL_ACTION)
            intent.putExtra(Constant.ACTION, ACTION_VOICE_NORMAL)
            context.sendBroadcast(intent)
        }
    }


}